package com.cn.demo4.common;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

public class Tool {
    public static Integer stringToInt(String value) {
        int result = 0;
            try {
                result = Integer.parseInt(value);
            } catch (Exception e) {
//                e.printStackTrace();
            }
        return result;
    }


    public static Double stringToDouble(String value) {
        Double result = 0.0;
        try {
            result = Double.parseDouble(value);
        } catch (Exception e) {
//            e.printStackTrace();
        }
        return result;
    }

    /**
     * curPage页码默认为1
     * @param value
     * @return
     */
    public static Integer toCurPage(String value) {
        int result = 1;
        try {
            result = Integer.parseInt(value);
        } catch (Exception e) {
//            e.printStackTrace();
        }
        return result;
    }

    /**
     * pageSize默认每页显示10条
     * @param value
     * @return
     */
    public static Integer toPageSize(String value) {
        int result = 10;
        try {
            result = Integer.parseInt(value);
        } catch (Exception e) {
//            e.printStackTrace();
        }
        return result;
    }
    /**
     * 中文字符编码
     * 跨域
     */
    public static void setEncoding(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        /* 允许跨域的主机地址 */
        resp.setHeader("Access-Control-Allow-Origin", "http://localhost:63342");
        /* 允许跨域的请求方法GET, POST, HEAD 等 */
        resp.setHeader("Access-Control-Allow-Methods", "*");
        /* 重新预检验跨域的缓存时间 (s) */
        resp.setHeader("Access-Control-Max-Age", "3600");
        /* 允许跨域的请求头 */
        resp.setHeader("Access-Control-Allow-Headers", "*");
        /* 是否携带cookie */
        resp.setHeader("Access-Control-Allow-Credentials", "true");
        resp.setContentType("");
    }


}
